<template>
  <div>
    <div class="clearfix">
      <el-button
        size="mini"
        class="float-right"
        type="primary"
        @click="addBtnClick">
        <i class="el-icon-plus"/>
      </el-button>
    </div>
    <br>
    <!--列表数据-->
    <w-cell
      v-for="(v, k) in dataArr"
      :link="v.link"
      :title="v.title"
      :describe="v.link"
      :hover="false"
      :operation-width="120"
      :key="k">
      <el-button
        type="primary"
        size="mini"
        class="el-icon-edit"
        @click.stop="editBtnClick(v)"/>
      <el-button
        type="danger"
        size="mini"
        class="el-icon-delete"
        @click.stop="detailBtnClick(v)"/>
    </w-cell>

    <!--添加-->
    <el-dialog
      :title="moduleTitle"
      :visible.sync="dialogFormVisible"
      width="600px">
      <el-form :model="form">
        <el-form-item>
          <el-input
            v-model.lazy="form.title"
            clearable
            placeholder="请输入">
            <template slot="append">标题</template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="form.link"
            clearable
            placeholder="请输入">
            <template slot="append">链接</template>
          </el-input>
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer">
        <el-button
          size="mini"
          @click="cancelBtnClick">取 消</el-button>
        <el-button
          :loading="loading"
          :disabled="!form.title"
          type="primary"
          size="mini"
          @click="okBtnClick">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import HomeLinksModel from '@/models/web/HomeLinksModel'
import WCell from '@/components/pc/Cell/cell_02'
export default {
  name: 'AdminWebHomeLinks',
  components: {
    WCell
  },
  data() {
    return {
      /**
       * 是否显示弹窗
       */
      dialogFormVisible: false,
      /**
       * 弹窗from
       */
      form: new HomeLinksModel(),
      /**
       * 数据数组
       */
      dataArr: [],
      /**
       * 是否是编辑模式
       */
      isEdit: false,
      /**
       * 是否加载中
       */
      loading: false
    }
  },
  computed: {
    /**
     * 模态框标题
     * @returns {string}
     */
    moduleTitle() {
      return this.isEdit ? '编辑HomeLink' : '添加HomeLink'
    }
  },
  mounted() {
    this.getAllModels()
  },
  methods: {
    /**
     * 添加按钮点击
     */
    addBtnClick() {
      this.isEdit = false
      this.form = new HomeLinksModel()
      // 添加弹窗
      this.dialogFormVisible = true
    },
    /**
     * 确定按钮点击
     */
    okBtnClick() {
      this.loading = true
      this.form.addOrUpdate(this.isEdit).then(() => {
        this.dialogFormVisible = false
        this.getAllModels()
      }).catch(() => {
      }).finally(() => {
        this.loading = false
      })
    },
    /**
     * 取消按钮点击
     */
    cancelBtnClick() {
      this.dialogFormVisible = false
    },
    /**
     * 获取所有数据
     */
    getAllModels() {
      HomeLinksModel.loadAllModels().then((dataArr) => {
        this.dataArr = dataArr
      })
    },
    /**
     * 编辑按钮点击
     * @param index
     * @param row
     */
    editBtnClick(item) {
      this.isEdit = true
      this.form = new HomeLinksModel()
      Object.assign(this.form, item)
      this.dialogFormVisible = true
    },
    /**
     * 删除按钮点击
     * @param item
     */
    detailBtnClick(item) {
      item.dustbin_push('title').then(() => {
        this.getAllModels()
      }).catch(() => {})
    },
    /**
     * 图片显示
     * @param src
     */
    imageShowClick(src) {
      this.$refs['IMGE_SHOW'].show([src])
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
